<!-- 采购部：评审资料建档——蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex">
          <h1 class="f16">建档资料明细</h1>
          <JDialog :tableHeader="tableHeader" :tableData="tableData" :width="85" @confirm="dialogConfirm" @removeOne="removeOne" />
          <span style="margin-top: 10px; margin-left: 10px; color: red">ps:双击某一列可按照该列排序</span>
        </div>

        <Table
          @confirmChange="confirmChange"
          :tableHeader="tableHeader"
          :tableData="tableData"
          selection
          @currentRow="currentRow"
          @selectList="selectTableList"
          @currentRow1="dbClick"
          :isLoading="isLoading"
        >
        </Table>
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { selectList } from '@/plugins/mixin'
import { formateData } from '@/plugins/mixinJ'
import LoginWork from '@/utils/LoginWork'
import fileRequest from '@/utils/fileRequest'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ReviewDataFile'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const totalSelect = ref(0)
const pageSizeSelect = ref(40)
const currentPageSelect = ref(1)
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    PageModule: true,
    handleSizeChange: (val: number) => {
      pageSizeSelect.value = val
      getSelectList()
    },
    handleCurrentChange: (val: number) => {
      currentPageSelect.value = val
      getSelectList()
    },
    total: totalSelect,
    selectValue: '',
    selectP: '型体',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '楦头',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '客人交期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '勾选复制'
  },
  {
    type: 'button',
    checkboxLabel: '更新指令'
  },
  {
    type: 'button',
    checkboxLabel: '打勾导出'
  }
])
const tableHeader = ref([
  {
    change: true,
    label: '指令',
    prop: 'lotNo'
  },
  {
    change: true,
    label: '品牌',
    prop: 'custom'
  },
  {
    change: true,
    label: '型体',
    prop: 'style'
  },
  {
    change: true,
    label: '颜色',
    prop: 'color'
  },
  {
    change: true,
    label: '楦头',
    prop: 'lastNo'
  },
  {
    change: true,
    label: '订单数',
    prop: 'orderQty'
  },
  {
    change: true,
    type: 'dateSingle2',
    label: '下单日期',
    prop: 'orderDate'
  },
  {
    change: true,
    type: 'dateSingle2',
    label: '客人交期',
    prop: 'deliveryDate'
  },
  {
    label: '主面料采购周期',
    prop: 'upperMtlLt'
  },
  {
    label: '大底采购周期',
    prop: 'soleMtlLt'
  },
  {
    label: '大底开模周期',
    prop: 'soleMoldLt'
  },
  {
    label: '外协工艺周期',
    prop: 'hfLt'
  },
  {
    label: '针车日人均产能',
    prop: 'stitchPpd'
  },
  {
    label: '成型日人均产能',
    prop: 'lastPpd'
  }
])

const getSelectList = async () => {
  const style = formList.value.find(
    (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
  ) as any
  style.selectOptions.splice(0)
  const res = (await service.post(`/back/getStyleQuery?style=${style?.selectValue}`, {
    pageNum: currentPageSelect.value,
    pageSize: pageSizeSelect.value
  })) as any
  if (res) {
    res.dtos.map((item: any, index: number) => {
      style?.selectOptions?.push({
        optionLabel: item,
        optionKey: index,
        optionValue: item
      })
    })
    totalSelect.value = res.total
  }
}
onMounted(async () => {
  await selectList(formList, () => service.get('/back/order/allOrderCondition'))
  getSelectList()
})

// 表一模块
// 表一按钮
const selectedLists = ref([])
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '勾选复制') {
    const res = (await LoginWork.post(`/ReviewMaterial/selectAndCopy?lotNos=${selectedLists.value}&lotNo=${currentTableRow.value.lotNo}`)) as any
    if (res.code === 200) {
      JMessage.success('复制成功')
      getTableList()
    } else {
      JMessage.error('复制失败')
    }
  }
  if (content === '更新指令') {
    const res = (await LoginWork.get('/ReviewMaterial/reviewMaterialAdd')) as any
    if (res.code === 200) {
      JMessage.success('修改成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '打勾导出') {
    const res = (await fileRequest.post(`/ReviewMaterial/exportReviewMaterial`, selectedLists.value)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async (sort = 'lotNo', sortNo = '0') => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/ReviewMaterial/getReviewMaterialList', {
    custom: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    deliveryDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[1],
    deliveryDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[0],
    lastNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '楦头')
      ?.selectValue,
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
    )?.inputValue,
    orderDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[1],
    orderDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[0],
    sort, //	排序字段
    sortNo, // 排序顺序（1降序0升序）
    style: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '型体')
      ?.selectValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.lotNoList.map((item: any) => {
    item.change = 'Y'
    // item.disableConfirm = true;
    return item
  })
  total.value = tableDataList.total
  currentRow()
}, 500)

watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
onMounted(() => {
  getTableList()
})
// 添加
const dialogConfirm = async (obj: any, dialogVisible: any) => {
  console.log(obj)
  // console.log(copyData[0].mtlProperty);
  const res = (await LoginWork.post(`/ReviewMaterial/addReviewMaterial`, obj)) as any
  if (res.code === 200) {
    JMessage.success('新增成功')
    dialogVisible.value = false
    getTableList()
  } else {
    JMessage.error('新增失败')
  }
}
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
// 删除
const removeOne = async () => {
  const res = (await LoginWork.post(`/ReviewMaterial/deleteReviewMaterial`, currentTableRow.value)) as any
  if (res.code === 200) {
    JMessage.success('删除成功')
    getTableList()
  } else {
    JMessage.error('删除失败')
  }
}
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList.map((item: any) => {
    return item.lotNo
  })
}
const confirmChange = async (row: any, fn: any) => {
  const res = (await LoginWork.post('/ReviewMaterial/reviewMaterialUpdate', [currentTableRow.value])) as any
  if (res.code === 200) {
    JMessage.success('修改成功')
    fn()
  } else {
    JMessage.error(res.message)
  }
}
let tem = '1'
const dbClick = (row: any, prop: any) => {
  tem === '1' ? getTableList(prop, '0') : getTableList(prop, '1')
  tem === '1' ? (tem = '0') : (tem = '1')
}
</script>

<style lang="less" scoped>
table {
  table-layout: auto !important;
}

:deep(.el-table__header) {
  width: 100% !important;
}

:deep(.el-table__body) {
  width: 100% !important;
}
</style>
